package com.zzty.txj1.controller;

import com.zzty.txj1.entity.Event;
import com.zzty.txj1.service.EventService;
import com.zzty.txj1.util.SplitUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/event")
public class EventController {
    private static final Logger logger = LoggerFactory.getLogger(EventController.class);

    @Autowired
    EventService eventService;

    @RequestMapping(value = "/addEvent", method = RequestMethod.POST)
    public Map addEvent(Event event) {
        Map<String, String> map = new HashMap<>();
        String result = "false";
        Integer flag = eventService.addEvent(event);
        logger.info("Event--controller ============ addEvent flag :" + flag);
        if (1 == flag) {
            result = "success";
        }
        map.put("result", result);
        return map;
    }

    @RequestMapping(value = "allEvent")
    public List allEvent() throws Exception {
        List<Event> events = new ArrayList<>();
        events = eventService.allEvent();
        logger.info("Event--controller ============ allEvent events :" + events);
        return events;
    }

    @RequestMapping(value = "/deleteEventById", method = RequestMethod.POST)
    public Map deleteEventById(Event event) {
        logger.info("Event--controller ============ deleteEventById id :" + event.getId());
        Integer id = event.getId();
        Map<String, String> map = new HashMap<>();
        String result = "false";
        Integer flag = eventService.deleteEventById(id);
        logger.info("Event--controller ============ deleteEventById flag :" + flag);
        if (1 == flag) {
            result = "success";
        }
        map.put("result", result);
        return map;
    }

    @RequestMapping(value = "/getEventById", method = RequestMethod.POST)
    public Event getEventById(Event event) {
        System.out.println("------------controller in :" + event.toString());
        Event event1 = eventService.getEventById(event);
        logger.info("Event--controller ============ getEventById event1 :" + event1.toString());
        return event1;
    }

    @RequestMapping(value = "/getLocationsByEventName", method = RequestMethod.POST)
    public Map<String, Object> getLocationsByEventName(Event event) throws Exception {
        Map<String, Object> results = new HashMap<>();
        String result = "false";
        logger.info("Event--controller ============ getLocationsByEventName event :" + event.toString());
        List<Integer> locationList = null;
        String locations = eventService.getLocationsByEventName(event);
        logger.info("Event--controller ============ getLocationsByEventName locations :" + locations);
        SplitUtil splitUtil = new SplitUtil();
        if (locations != null && !locations.equals("") && !locations.equals("null")) {
            locationList = new ArrayList<>();
            locationList = splitUtil.getSplit(locations);
        }
        logger.info("Event--controller ============ getLocationsByEventName locationList2 :" + locationList);
        if (locationList != null) {
            result = "success";
        }
        results.put("result", result);
        results.put("locations", locationList);
        return results;

    }

    @RequestMapping(value = "/updateEventById", method = RequestMethod.POST)
    public Map updateEventById(Event event) {
        Map<String, String> map = new HashMap<>();
        String result = "false";
        Integer count = eventService.updateLocationById(event);
        if (1 == count) {
            result = "success";
        }
        logger.info("Event--controller ============ updateEventById map :" + map);
        map.put("result", result);
        return map;
    }

    @RequestMapping(value = "/getEventsById", method = RequestMethod.POST)
    public Map getEventsById(Event event) {
        Map<String, String> map = new HashMap<>();
        Event event1 = eventService.getEventById(event);
        map.put("eventName", event1.getName());
        map.put("startTime", event1.getStartTime());
        map.put("endTime", event1.getEndTime());
        map.put("money", event1.getMoney().toString());
        logger.info("Event--controller ============ getEventsById map :" + map);
        return map;
    }

    @RequestMapping("/getEventIdByName")
    public Integer getEventIdByName(Event event) {
        Integer flag = null;
        flag = eventService.getEventIdByName(event.getName());
        logger.info("Event--controller ============ getEventIdByName flag :" + flag);
        return flag;
    }


}
